home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Floppyshop 2
/
Floppyshop - 2.zip
/
Floppyshop - 2.iso
/
art&graf.ix
/
art-0039
/
source
/
qsort.def
< prev
next >
Wrap
Text File
|
1997-04-16
|
6KB
|
98 lines
DEFINITION MODULE QSort;
(*---------------------------------------------------------------------*)
(* A Generic Sorting Module - based on code taken from the *)
(* book - Software Development with Modula-2 by Ford & Weiner. *)
(* *)
(* The sort procedure will allow the user to to provide the *)
(* compare procedure or will sort using standard data types. *)
(* Multiple keys will NOT be allowed. *)
(* *)
(* It will only sort arrays in memory. *)
(* *)
(* The sort is not stable ( duplicate keys will NOT remain in the *)
(* original order that they were in the array. *)
(* *)
(* I would like to be able to provide the user of the routines in *)
(* this module complete freedom to sort any type of data. To do this *)
(* The user will have to supply a 'compare' procedure plus *)
(* information about the size of elements in the array. *)
(* I would also like to provide a sort routine suitable for the user *)
(* who just wants to sort a simple array. I would also like to be *)
(* able sort a record where the key is not at the start of the *)
(* record, but is still a standard data type ( not implemented! ) *)
(* *)
(* Two routines are provided; one routine will sort an array of *)
(* a standard data type: CARDINAL, INTEGER, REAL, LONGCARD and *)
(* LONGINT. *)
(* *)
(* The parameters passed are: *)
(* 1) The fixed length array to be sorted. *)
(* 2) The number of elements to sort. *)
(* 3) the type of data in the arry. *)
(* *)
(* The other routine provided can be used to sort any array in memory.*)
(* You provide it with a routine to compare two elements in the array.*)
(* The routine must use pointers to the objects to be compared as *)
(* the addresses of the elements are provided. the routine MUST do *)
(* a 'LESS THAN' or GREATER THAN' test. It will return TRUE if the *)
(* elements are in order. If it does a '<=' or '>=' test then the *)
(* sort will loop! *)
(* *)
(* The parameters passed are: *)
(* 1) The fixed length array to be sorted. *)
(* 2) One element so the size of the element can be found easily. *)
(* 3) The number of elements to sort. *)
(* 4) the compare routine. *)
(* *)
(* Notes on the implementation: *)
(* *)
(* The objects to be sorted are considered to be an array of BYTES, *)
(* each object being of fixed length. *)
(* *)
(* The array will be sorted in place. *)
(* *)
(* The algorithm used will be 'quicksort'. There are some things *)
(* to be aware of when using this algorithm; it is very recursive, *)
(* so on a bad day it can use an awful lot of stack space. It can be *)
(* very slow if the input data is nearly ordered. In fact, it is *)
(* better to mess up the data in the case of an ordered list as the *)
(* sort will be a lot quicker, strange huh?! *)
(* *)
(* This implementation uses selection sort for small partitions, *)
(* this is quicker than quicksort and helps reduce the depth of *)
(* recursion. The SMALLER partition is always sorted first, this *)
(* is a great help in preventing the depth of recursion getting *)
(* to great. The 'pivot' value is found by looking at a small sample *)
(* of the elements. This reduces the chances on picking 'bad' values.*)
(* *)
(* Given the above why do I use it? It is FAST! There is no known *)
(* faster sort, on random sets of data, when comparing keys. *)
(* *)
(* 1/ 9/89 LGM : Original *)
(*---------------------------------------------------------------------*)
FROM SYSTEM IMPORT BYTE, ADDRESS;
TYPE
SortKeyType = (cardinal, integer, real, longcard, longint);
CompareProc =
PROCEDURE ( ADDRESS , ADDRESS ) : BOOLEAN;
(* must return TRUE iff First^ > Second^ *)
PROCEDURE SortArray
( VAR ObjectArray : ARRAY OF BYTE;
NumberOfElements : LONGCARD;
TypeOfDataInKey : SortKeyType );
PROCEDURE SortArrayWithKeys
( VAR ObjectArray : ARRAY OF BYTE;
VAR ExampleObject : ARRAY OF BYTE;
NumberOfElements : LONGCARD;
CompareKeys : CompareProc );
END QSort.